<div id="webcam"></div>

<center><a href="javascript:webcam.capture(3);void(0);"><?php echo $this->lang->line('galery_capturetime'); ?></a>
 &middot; <a href="javascript:webcam.capture();void(0);"><?php echo $this->lang->line('galery_capturenow'); ?></a></center>

<script type="text/javascript">
$(function() {

    var pos = 0, ctx = null, saveCB, image = [];

    var canvas = document.createElement("canvas");
	canvas.setAttribute('width', 320);
    canvas.setAttribute('height', 240);
    
    if (canvas.toDataURL) {

            ctx = canvas.getContext("2d");
            
            image = ctx.getImageData(0, 0, 320, 240);
    
            saveCB = function(data) {
                    
                    var col = data.split(";");
                    var img = image;

                    for(var i = 0; i < 320; i++) {
                            var tmp = parseInt(col[i]);
                            img.data[pos + 0] = (tmp >> 16) & 0xff;
                            img.data[pos + 1] = (tmp >> 8) & 0xff;
                            img.data[pos + 2] = tmp & 0xff;
                            img.data[pos + 3] = 0xff;
                            pos+= 4;
                    }

                    if (pos >= 4 * 320 * 240) {
                            ctx.putImageData(img, 0, 0);
                            $.post("<?php echo base_url(); ?>candidate/galery/upload", {type: "data", image: canvas.toDataURL("image/png")});
                            pos = 0;
                    }
            };

    } else {

            saveCB = function(data) {
                    image.push(data);
                    
                    pos+= 4 * 320;
                    
                    if (pos >= 4 * 320 * 240) {
                            $.post("<?php echo base_url(); ?>candidate/galery/upload", {type: "pixel", image: image.join('|')});
                            pos = 0;
                    }
            };
    }

    $("#webcam").webcam({

            width: 320,
            height: 240,
            mode: "callback",
            swffile: "<?php echo base_url(); ?>swf/jscam_canvas_only.swf",

            onSave: saveCB,

            onCapture: function () {
                    webcam.save();
            },

            debug: function (type, string) {
                    console.log(type + ": " + string);
            }
    });

});
</script>